<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

mm_showMyFileName( __FILE__ );

/*require_once(CLASSPATH . 'ps_product_attribute.php' );
$ps_product_attribute = new ps_product_attribute;
echo "<pre>";
var_dump($ps_product_attribute);
echo "</pre>";*/

$product_id = @JRequest::getInt("id",0);

$q = "SELECT * ";
$q.= "FROM #__vm_product a WHERE a.`product_id` = $product_id ";
$q.= "AND a.`product_publish` = 'Y' ";

$db =& JFactory::getDBO();
$db->setQuery($q);
$product = $db->loadObjectList();

$q = "SELECT * FROM #__vm_category WHERE `category_publish` = 'Y'";
$db = JFactory::getDBO();
$db->setQuery($q);
$category = $db->loadObjectList();
$category_id = @JRequest::getInt("category_id",$category[0]->category_id);

$q = "SELECT * FROM #__vm_product_price WHERE `product_id` = ".$product[0]->product_id;
$db->setQuery($q);
$product_price = $db->loadObjectList();			

$q = "SELECT * FROM #__vm_product_relations a WHERE a.`product_id` = $product_id";	
$db->setQuery($q);
$rel_key = $db->loadObjectList();
$relations = array();
$in_phrase = "";
if(count($rel_key) > 0){
	$temp = @str_replace("|",",",$rel_key[0]->related_products);
	if($temp != "") $in_phrase = "(" . $temp . ")";
	$q = "SELECT * FROM #__vm_product WHERE `product_id` IN $in_phrase";
	$db->setQuery($q);
	$relations = $db->loadObjectList();
}
$price_related = array();
for($i = 0;$i<count($relations);$i++){
	$q = "SELECT * FROM #__vm_product_price WHERE `product_id` = ".$relations[$i]->product_id;
	$db->setQuery($q);
	$p_temp = $db->loadObjectList();
	$price_related[$i] = $p_temp[0]->product_price;
}

$q = "SELECT * FROM #__vm_product_files WHERE `file_product_id` = ".$product[0]->product_id." AND `file_mimetype` = 'image/jpeg'";
$db->setQuery($q);
$more_image = $db->loadObjectList();
//var_dump($price_related);

$temp1 = explode(";",$product[0]->attribute);
$attribute = array();
for($i=0;$i<count($temp1);$i++){
	$temp2 = explode(",",$temp1[$i]);
	$attribute[$i] = $temp2;
}

$tpl = vmTemplate::getInstance();
$tpl->set("category_id",@$category_id);
$tpl->set("category",@$category);
$tpl->set("product",@$product);
$tpl->set("price", @$product_price);
$tpl->set("price_related",@$price_related);
$tpl->set("relations",@$relations);
$tpl->set("more_image",@$more_image);
$tpl->set("attribute",@$attribute);

$flypage = "shop.detail.tpl";
echo $tpl->fetch( '/pages/'.$flypage . '.php' );
?>